<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/quest.jsp"%>
<%
    // 查询科室版医生用信息
    String id = input.getDesc("id", "-1");
    Integer uid = input.getInt("uid", 0);
    List<Map> list = QBuilder.db("basedb").select("distinct new map (u.id as id, u.name as name, u.avatar as avatar, u.title as title, h.name as dept_name,h.descr as hospital_name, h.hospital as dept_id, h.parent as register_hospital_id, u.gender as gender, u.employeeId as employee_id,u.jsonString as json, u.type as type, u.phone as phone)").from("Hospital as h, User as u").where(new Where("h.status=", STATUS_ENABLE).and("h.hospital=u.deptId").and("u.type=", USER_EMPLOYEE).and("u.id=", id).and("h.type=", 2).and("u.status=", STATUS_ENABLE)).query().getResultObj();
    if (isEmpty(list)) {
        msg = new Message(30041, "没有医生信息");
        msg.put("data", new HashMap());
    } else {
        Map expertMap = list.get(0);
        Map jsonMap = JSONUtil.fromJson(expertMap.remove("json") + "", new TypeReference<Map>(){});
        User user = User.fromMap(expertMap, User.class);
        expertMap.put("avatar", getDefaultAvatar(user));
        expertMap.put("phone", Tools.getDesc(expertMap.get("phone"), ""));
        expertMap.put("specialty", Tools.getDesc(jsonMap.get("specialty"), "暂无擅长信息"));
        expertMap.put("introduce", Tools.getDesc(jsonMap.get("introduce"), "暂无介绍信息"));
        Where where  = new Where();
        List<Map> expertIds = QBuilder.db("basedb").select("new map(s.doctorId as expert_id,s.deptName as dept_name, s.doctorName as name, s.date as date, s.half as half)").from("Schedule as s").where(where.and("s.doctorId=", Tools.getDesc(expertMap.get("employee_id"))).and("s.registerType=2").and("s.hospital=", Tools.getInt(expertMap.get("register_hospital_id") + "", -1)).and("s.status = ", STATUS_ENABLE)).query().getResultObj();
        Map<String, String> times = new LinkedHashMap<String, String>();
        for (int i = 0; i < expertIds.size(); i++) {
            Map map = expertIds.get(i);
            String date = Tools.getDesc(map.remove("date"), "");
            String half = Tools.getDesc(map.remove("half"), "");
            if ("".equals(date) || "".equals(half)) {
                continue;
            }
            Date d = DateUtil.parseDate(date);
            String time = DateUtil.formatDateByFormat(d, "EEEE").replaceAll("星期", "周");
            String str = Tools.getDesc(times.get(time), "");
            if (str.contains(half)) {
                continue;
            }
            str += "_" + half;
            times.put(time, str);
        }
        Set<Entry<String, String>> set = times.entrySet();
        String result = "";
        for (Entry<String, String> entry : set) {
            String key = entry.getKey();
            String value = entry.getValue();
            result += key;
            String[] values = value.split("_");
            Arrays.sort(values);
            for(int i = 0; i < values.length; i++) {
                if ("".equals(values[i])) {
                    continue;
                }
                result += ("1".equals(values[i]) ? "上午" : ("2".equals(values[i]) ? "下午" : values[i])) + "、";
            }
            result = result.substring(0, result.length() > 1 ? result.length() - 1 : result.length());
            result += "，";
        }
        result = result.substring(0, result.length() > 1 ? result.length() - 1 : result.length());
        result += "。";
        if ("。".equals(result))
            result = "暂无出诊信息";
        expertMap.put("time", result);
        OperateHistory oh = new OperateHistory();
        oh.setUid(uid+"");
        oh.setHospital(hospital);
        oh.setOid(id);
        oh.setCategory(SECTION_DOCTOR);
        query(expertMap, oh);
        input.setDesc("uuid", id);
        input.setDesc("uid", uid + "");
        input.setDesc("range", "my");
        input.setDesc("item_count", "10");
        expertMap.put("quests", search(input).get("data"));
        // 使用号源排班时间
        Map<String, String> item = JSONUtil.fromJson(JSONUtil.toJson(times)
                .replaceAll("\"周一\"", "1").replaceAll("\"周二\"", "2")
                .replaceAll("\"周三\"", "3")   .replaceAll("\"周四\"", "4")
                .replaceAll("\"周五\"", "5")  .replaceAll("\"周六\"", "6")  .replaceAll("\"周日\"", "7"), new TypeReference<Map>(){});
        Map<String, Map<String, Integer>> scheduleTimeMap = new HashMap<>();
        for (int i = 1; i < 8; i++) {
            if(item.get(i + "") == null) {
                Map subItem = new HashMap();
                subItem.put("morning", 0);
                subItem.put("afternoon", 0);
                subItem.put("night", 0);
                scheduleTimeMap.put(i + "" , subItem);
            } else {
                String self =  Tools.getDesc(item.get(i + ""));
                Map subItem = new HashMap();
                subItem.put("morning", 0);
                subItem.put("afternoon", 0);
                subItem.put("night", 0);
                if (self.contains("1"))
                    subItem.put("morning", 1);
                if (self.contains("2"))
                    subItem.put("afternoon", 1);
                if (self.contains("3"))
                    subItem.put("night", 1);
                scheduleTimeMap.put(i  + "" , subItem);
            }
        }
        expertMap.put("time_table", scheduleTimeMap);
        // 使用真实的医生设置的门诊时间
        for (int i = 1; i < 8; i++) {
            Map subItem = new HashMap();
            subItem.put("morning", 0);
            subItem.put("afternoon", 0);
            subItem.put("night", 0);
            scheduleTimeMap.put(i + "", subItem);
        }
        expertMap.put("time_table", (jsonMap.get("time") == null || ((Map)jsonMap.get("time")).size() == 0)?scheduleTimeMap:jsonMap.get("time") );
        List reportAuthList = QBuilder.db("basedb").select().from("OperateHistory as o").where(new Where("o.hospital=", hospital).and("o.category=", SECTION_DOCTOR).and("o.operate=", AUTH_REPORT).and("o.status=", STATUS_ENABLE).and("o.uid=", uid).and("o.oid=", id)).query().getResultObj();
        expertMap.put("report_auth", 1);
        if (isEmpty(reportAuthList))
            expertMap.put("report_auth", 0);
        msg.put("data", expertMap);
    }
    Tools.encryptPrint(out, msg.toJson());
%>
